package gold.shovel;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class NC41 {
    public long startExecuteTime = System.currentTimeMillis();


    public class Solution {
        /**
         * @param arr int整型一维数组 the array
         * @return int整型
         */
        public int maxLength(int[] arr) {
            Map<Integer, Integer> posMap = new HashMap<>();
            int left = 0, right = 0, maxWindowLength = 0;
            while (right < arr.length) {
                if (posMap.containsKey(arr[right])) {
                    left = Math.max(left, posMap.get(arr[right]) + 1);
                }

                posMap.put(arr[right], right);
                maxWindowLength = Math.max(maxWindowLength, right++ - left + 1);
            }

            return maxWindowLength;
        }
    }

    public void run() {
        System.out.println(new Solution().maxLength(InputUtil.toIntegerArray("[3,3,2,1,3,3,3,1]")));
        System.out.println(new Solution().maxLength(InputUtil.toIntegerArray("[2,3,4,5]")));
        System.out.println(new Solution().maxLength(InputUtil.toIntegerArray("[2,2,3,4,3]")));
        System.out.println(new Solution().maxLength(InputUtil.toIntegerArray("[9]")));
        System.out.println(new Solution().maxLength(InputUtil.toIntegerArray("[1,2,3,1,2,3,2,2]")));
        System.out.println(new Solution().maxLength(InputUtil.toIntegerArray("[2,2,3,4,8,99,3]")));

    }

    public static void main(String[] args) throws Exception {
        NC41 an = new NC41();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
